package com.spark.incoicings.conroller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spark.incoicings.service.*;
import com.spark.invoicings.entity.*;
import com.spark.invoicings.spark_enum.SparkEnum;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/demand")
@Slf4j
public class PurchaseArrivalController {

    @Resource
    private PurchaseArrivalService purchaseArrivalService;

    @Resource
    private PurchaseDetailService purchaseDetailService;

    @Resource
    private ReceiptDetailService receiptDetailService;

    @Resource
    private ProductService productService;

    @Resource
    private OrderService orderService;

    @Resource
    private InventorysService inventorysService;

    /**
     * 根据id更新采购到货单状态
     * @param purchaseArrival
     * @return
     */
    @PostMapping("/updatePurchaseArrivalByOId")
    public Result updatePurchaseArrivalByOId(@RequestBody PurchaseArrival purchaseArrival) {
        log.info("updatePurchaseArrivalByOId purchaseArrival={}", purchaseArrival);
        return purchaseArrivalService.updatePurchaseArrivalByOId(purchaseArrival);
    }

    /**
     * 根据id获取产品
     * @param id
     * @return
     */
    @GetMapping("/selectProductByPId")
    public Result selectProductByPId(@RequestParam Integer id) {
        Product data = productService.selectProductByPId(id);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id删除盘点单
     * @param id
     * @return
     */
    @GetMapping("/delInventorysById")
    public Result delInventorysById(@RequestParam Integer id) {
        if(inventorysService.removeById(id)) return Result.success();
        return Result.error(SparkEnum.DELETE_EXCEPTION.getCode(), SparkEnum.DELETE_EXCEPTION.getMsg());
    }

    /**
     * 更新盘点单
     * @param inventorys
     * @return
     */
    @PostMapping("/updateInventorysById")
    public Result updateInventorysById(@RequestBody Inventorys inventorys) {
        if(inventorysService.updateInventorysById(inventorys)) return Result.success();
        return Result.error(SparkEnum.UPDATE_EXCEPTION.getCode(), SparkEnum.UPDATE_EXCEPTION.getMsg());
    }

    /**
     * 根据id获取盘点单
     * @param id
     * @return
     */
    @GetMapping("/selectInventorysById")
    public Result selectInventorysById(@RequestParam Integer id) {
        Inventorys data = inventorysService.selectInventorysById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 添加盘点单
     * @param inventorys
     * @return
     */
    @PostMapping("/addInventorys")
    public Result addInventorys(@RequestBody Inventorys inventorys) {
        return inventorysService.addInventorys(inventorys);
    }

    /**
     * 删除库存产品
     * @param id
     * @return
     */
    @GetMapping("/delProductById")
    public Result delProductById(@RequestParam Integer id) {
        if(productService.delProductById(id)) return Result.success();
        return Result.error(SparkEnum.DELETE_EXCEPTION.getCode(), "删除失败!! 该产品有关联数据");
    }

    /**
     * 查询所有盘点单
     * @param pageNum
     * @param pageSize
     * @param wName
     * @return
     */
    @GetMapping("/selectInventorysAll")
    public Result selectInventorysAll(@RequestParam(defaultValue = "1")
                                   Integer pageNum,
                                   @RequestParam(defaultValue = "10")
                                   Integer pageSize,
                                   @RequestParam(required = false)
                                   String wName) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("wName", wName);
        List<Inventorys> list = inventorysService.selectInventorysAll(map);
        PageInfo<Inventorys> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id更新产品
     * @param product
     * @return
     */
    @PostMapping("/updateProduct")
    public Result updateProduct(@RequestBody Product product) {
        if(productService.updateById(product)) return Result.success();
        return Result.error(SparkEnum.UPDATE_EXCEPTION.getCode(), SparkEnum.UPDATE_EXCEPTION.getMsg());
    }

    /**
     * 根据id回显产品
     * @param id
     * @return
     */
    @GetMapping("/selectPurchaseProductById")
    public Result selectPurchaseProductById(@RequestParam Integer id) {
        Product data = productService.selectPurchaseProductById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 添加产品
     * @param product
     * @return
     */
    @PostMapping("/addProduct")
    public Result addProduct(@RequestBody Product product) {
        return productService.addProduct(product);
    }

    /**
     * 查询所有库存
     * @param pageNum
     * @param pageSize
     * @param pName
     * @return
     */
    @GetMapping("/selectProductAll")
    public Result selectProductAll(@RequestParam(defaultValue = "1")
                                   Integer pageNum,
                                   @RequestParam(defaultValue = "10")
                                   Integer pageSize,
                                   @RequestParam(required = false)
                                   String pName) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("pName", pName);
        List<Product> list = productService.selectProductAll(map);
        PageInfo<Product> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id删除到货明细
     * @param id
     * @return
     */
    @GetMapping("/delReceiptDetail")
    public Result delReceiptDetail(@RequestParam Integer id) {
        if(receiptDetailService.removeById(id)) return Result.success();
        return Result.error(SparkEnum.DELETE_EXCEPTION.getCode(), SparkEnum.DELETE_EXCEPTION.getMsg());
    }

    /**
     * 更新到货单明细
     * @param receiptDetail
     * @return
     */
    @PostMapping("/updateReceiptDetail")
    public Result updateReceiptDetail(@RequestBody ReceiptDetail receiptDetail) {
        if (receiptDetailService.updateReceiptDetail(receiptDetail)) return Result.success();
        return Result.error(SparkEnum.UPDATE_EXCEPTION.getCode(),
                SparkEnum.UPDATE_EXCEPTION.getMsg());
    }

    /**
     * 根据id获取到货明细
     * @param id
     * @return
     */
    @GetMapping("/selectReceiptDetailByIds")
    public Result selectReceiptDetailByIds(@RequestParam Integer id) {
        ReceiptDetail data = receiptDetailService.selectReceiptDetailByIds(id);
        return Result.success(200, "success", data);
    }

    /**
     * 添加到货单明细
     * @param receiptDetail
     * @return
     */
    @PostMapping("/addReceiptDetail")
    public Result addReceiptDetail(@RequestBody ReceiptDetail receiptDetail) {
        return receiptDetailService.addReceiptDetail(receiptDetail);
    }

    /**
     * 根据id获取采购单信息
     * @param id
     * @return
     */
    @GetMapping("/selectOrderById")
    public Result selectOrderById(@RequestParam Integer id) {
        Map<String, Object> data = new HashMap<>();
        Order order = orderService.selectOrderById(id);
        List<PurchaseDetail> purchaseDetailList = purchaseDetailService.selectDetailById(order.getPurchaseOrderId());
        data.put("order",order);
        data.put("purchaseDetailList",purchaseDetailList);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id获取采购单信息
     * @param id
     * @return
     */
    @GetMapping("/selectDetailById")
    public Result selectDetailById(@RequestParam Integer id) {
        List<PurchaseDetail> data = purchaseDetailService.selectDetailById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 查询所有到货单明细
     * @param pageNum
     * @param pageSize
     * @param pName
     * @return
     */
    @GetMapping("/selectReceiptDetailAll")
    public Result selectReceiptDetailAll(@RequestParam(defaultValue = "1")
                                         Integer pageNum,
                                         @RequestParam(defaultValue = "10")
                                         Integer pageSize,
                                         @RequestParam(required = false)
                                         String pName) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("pName", pName);
        List<ReceiptDetail> list = receiptDetailService.selectReceiptDetailAll(map);
        PageInfo<ReceiptDetail> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id删除到货单
     * @return
     */
    @PostMapping("/delPurchaseArrival")
    public Result delPurchaseArrival(@RequestParam Integer id) {
        if(purchaseArrivalService.delPurchaseArrival(id)) return Result.success();
        return Result.error(SparkEnum.DELETE_EXCEPTION.getCode(), "删除失败! 该到货单下面有到货明细单 或 联系管理员!");
    }

    /**
     * 根据id获取采购到货单
     * @param id
     * @return
     */
    @GetMapping("/selectPurchaseArrivalById")
    public Result selectPurchaseArrivalById(@RequestParam Integer id) {
        PurchaseArrival data = purchaseArrivalService.selectPurchaseArrivalById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 根据id更新采购到货单状态
     * @param purchaseArrival
     * @return
     */
    @PostMapping("/updatePurchaseArrivalStatusById")
    public Result updatePurchaseArrivalStatusById(@RequestBody PurchaseArrival purchaseArrival) {
        purchaseArrivalService.updateById(purchaseArrival);
        return Result.success(200, "success", purchaseArrival);
    }

    /**
     * 根据id更新采购到货单
     * @param purchaseArrival
     * @return
     */
    @PostMapping("/updatePurchaseArrival")
    public Result updatePurchaseArrival(@RequestBody PurchaseArrival purchaseArrival) {
        return purchaseArrivalService.updatePurchaseArrival(purchaseArrival);
    }

    /**
     * 根据id获取采购明细
     * @param id
     * @return
     */
    @GetMapping("/selectPurchaseDetailsById")
    public Result selectPurchaseDetailsById(@RequestParam Integer id) {
        PurchaseDetail data = purchaseDetailService.selectPurchaseDetailsById(id);
        return Result.success(200, "success", data);
    }

    /**
     * 添加采购到货单
     * @param purchaseArrival
     * @return
     */
    @PostMapping("/addPurchaseArrival")
    public Result addPurchaseArrival(@RequestBody PurchaseArrival purchaseArrival) {
        return purchaseArrivalService.addPurchaseArrival(purchaseArrival);
    }

    /**
     * 查询所有采购到货单
     * @param pageNum
     * @param pageSize
     * @param sName
     * @return
     */
    @GetMapping("/selectPurchaseArrivalAll")
    public Result selectPurchaseArrivalAll(@RequestParam(defaultValue = "1")
                                    Integer pageNum,
                                    @RequestParam(defaultValue = "10")
                                    Integer pageSize,
                                    @RequestParam(required = false)
                                    String sName) {
        PageHelper.startPage(pageNum, pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("sName", sName);
        List<PurchaseArrival> list = purchaseArrivalService.selectPurchaseArrivalAll(map);
        PageInfo<PurchaseArrival> data = new PageInfo<>(list);
        return Result.success(200, "success", data);
    }

}
